/*
 * @Author: tcosfish
 * @Date: 2022-07-27 15:03:23
 * @LastEditors: tcosfish
 * @LastEditTime: 2022-08-17 11:02:06
 * @FilePath: \loadFile\src\excel\feat\create_month_repo.ts
 */

import ExcelJS from "exceljs"
import { tableTemplate } from "../utils/tableTemplate.util"
import ry_excel from "@/excel/RyExcel"
import ry_database from "@/database/RyDataBase"
import { SubjectBalanceView } from "@/database/views/SubjectBalance.view"
import { CostDetailsView } from "@/database/views/CostDetails.view"

import { join } from "path"

/**
 * @description: 将数据按照指定格式输出
 * @param {any} data 筛选后的数据
 * @param {string} save_filepath 文件保存的路径
 * @return {*}
 */
async function create_month_repo(data: any[], save_filepath: string) {
  const wb = new ExcelJS.Workbook()
  const sht = wb.addWorksheet("3.科目余额表", {
    properties: { defaultColWidth: 40, defaultRowHeight: 20 },
    pageSetup: {
      showGridLines: true,
      horizontalCentered: true,
      verticalCentered: true,
    },
  })

  const cell_A1 = sht.getCell("A1")
  cell_A1.value = "截止至 2022.07.27"
  cell_A1.font = {
    bold: true,
    size: 16,
  }
  sht.mergeCells("A1:B1")

  // ! 求和在数据录入之后
  sht.addTable(
    tableTemplate(
      "应收账款表",
      "A2",
      [
        { name: "名称", totalsRowFunction: "none", totalsRowLabel: "总计" },
        { name: "期末借方余额", totalsRowFunction: "sum" },
        { name: "期末贷方余额", totalsRowFunction: "sum" },
        { name: "借方余额 - 贷方余额" },
      ],
      data
    )
  )

  sht.eachRow((row) => {
    row.alignment = { vertical: "middle", horizontal: "center" }
  })

  await wb.xlsx.writeFile(save_filepath)
  console.log("月报已生成")
}

// 把函数编写当成思维导图的建立对待
// 下一步便是通过配置而不是一大串
async function createMonthlyReport(saveFilePath: string, reportOptions?: any) {
  function templatePath(name: string) {
    return join(
      __dirname,
      "../../../",
      `public/uploads/xlsx/template/${name}.xlsx`
    )
  }
}

function createCols(tableName: string) {
  return [
    { name: "名称", totalsRowFunction: "none", totalsRowLabel: "总计" },
    { name: "期末借方余额", totalsRowFunction: "sum" },
    { name: "期末贷方余额", totalsRowFunction: "sum" },
    { name: tableName },
  ]
}

function createCols2(tableName: string) {
  return [
    { name: "名称", totalsRowFunction: "none", totalsRowLabel: "总计" },
    { name: "本期借方发生额", totalsRowFunction: "sum" },
    { name: tableName },
  ]
}

export { create_month_repo, createMonthlyReport }
